package com.deyang.web.controller.system;

import com.deyang.common.core.controller.BaseController;
import com.deyang.common.core.domain.AjaxResult;
import com.deyang.common.core.domain.entity.SysDictData;
import com.deyang.common.core.page.TableDataInfo;
import com.deyang.common.utils.StringUtils;
import com.deyang.common.utils.uuid.IdUtils;
import com.deyang.system.domain.ResidentRegistInfo;
import com.deyang.system.domain.vo.ResidentRegistVo;
import com.deyang.system.service.IResidentRegistInfoService;
import com.deyang.system.service.ISysDictTypeService;
import com.github.pagehelper.PageHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 居民登记信息Controller
 *
 * @author dingqianbin
 * @date 2021-08-06
 */
@Controller
@RequestMapping("/system/epidemic")
public class EpidemiacWarController extends BaseController {


    @Autowired
    private IResidentRegistInfoService residentRegistInfoService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 新增保存居民登记信息
     */
    @ResponseBody
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody ResidentRegistInfo residentRegistInfo) {
        residentRegistInfo.setId(IdUtils.simpleUUID());
        residentRegistInfo.setCreateTime(new Date());
        residentRegistInfo.setUpdateTime(new Date());
        if (residentRegistInfoService.insertResidentRegistInfo(residentRegistInfo) == 0) {
            return AjaxResult.error("新增居民登记信息失败");
        }
        return AjaxResult.success("新增居民登记信息成功");
    }

    /**
     * 修改居民登记信息
     */
    @ResponseBody
    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody ResidentRegistInfo residentRegistInfo) {
        if (residentRegistInfoService.updateResidentRegistInfo(residentRegistInfo) == 0) {
            return AjaxResult.error("修改居民登记信息失败");
        }
        return AjaxResult.success("修改居民登记信息成功");
    }

    /**
     * 查看居民登记信息
     */
    @ResponseBody
    @GetMapping("query")
    public AjaxResult query(@RequestParam("id") String id) {
        return AjaxResult.success(residentRegistInfoService.selectResidentRegistInfoById(id));
    }

    /**
     * 查看居民登记信息列表
     */
    @ResponseBody
    @PostMapping("list")
    public TableDataInfo list(@Validated @RequestBody ResidentRegistVo residentRegistVo) {
        PageHelper.startPage(residentRegistVo.getPageNum(), residentRegistVo.getPageSize());
        List<ResidentRegistInfo> list = residentRegistInfoService.selectResidentRegistList(residentRegistVo);
        return getDataTable(list);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ResponseBody
    @GetMapping(value = "type")
    public AjaxResult dictType(@RequestParam("dictType") String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }


    /**
     * 统计-社区防控
     */
    @ResponseBody
    @GetMapping("queryCommunity")
    public AjaxResult queryCommunity() {
        return AjaxResult.success(residentRegistInfoService.selectCommunity());
    }




    /**
     * 查看社区防控详细信息列表
     */
    @ResponseBody
    @GetMapping("listCommunity")
    public TableDataInfo listCommunity(@RequestParam("state") String state,@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ResidentRegistInfo> list = residentRegistInfoService.listCommunity(state.toString());
        return getDataTable(list);
    }




    /**
     * 数据导出
     * auth:zh
     * date:2019/7/1
     *
     * @param map
     * @param request
     * @param response
     */
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        //先判断传过来的mapList是否是空，如果是空的话,导出全部,选中的话，取出id然后批量查询导出
        String fileName = "重点地区返回人员健康监测台账";
        HSSFWorkbook wb = residentRegistInfoService.export();
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
        }
    }

    /**
     * 功能描述:发送响应流方法
     *
     * @author zh
     * @date 2019/8/15
     */
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        String fileName1 = "重点地区返回人员健康监测台账";
        try {
            try {
                fileName = new String(fileName.getBytes("utf-8"), "ISO_8859_1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName+".xls");
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
        }
    }
}
